package com.weekdo.moviecomic;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

public class moviecomic extends Activity implements OnItemClickListener {

	private MainMenu menuAdapter;
	private String[] thumbPathList;
	private String[] comicNameList;
	private ComicCenter comicCenter;

	@Override
	public void onStart() {
		super.onResume();
		loadData();
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		FileManager.tryCreateDirs(FileManager.MCK_BASE_DIR);
		FileManager.tryCreateDirs(FileManager.MCK_VIEW_CACHE_DIR);
		FileManager.tryCreateDirs(FileManager.MCK_ARCHIVE_DIR);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		((Button) findViewById(R.id.searchIntroButton)).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent searcher = new Intent(v.getContext(), SearchMovie.class);
				startActivity(searcher);
			}
		});
		
		((Button) findViewById(R.id.deleteButton)).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				try {
					FileManager.delDirs(FileManager.MCK_ARCHIVE_DIR);
					FileManager.delDirs(FileManager.MCK_VIEW_CACHE_DIR);
					loadData();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

	}
	
	private void loadData() {
		comicCenter = new ComicCenter();
		String[] comicEntryList = comicCenter.getComicEntryList();
		GridView gv = (GridView) findViewById(R.id.gride);
		
		thumbPathList = new String[comicEntryList.length];
		comicNameList = new String[comicEntryList.length];
		for (int i=0; i<comicEntryList.length; i++)
		{
			thumbPathList[i] = getComicPosterPathByEntry(comicEntryList[i]);
			comicNameList[i] = getComicNameByEntry(comicEntryList[i]);
		}
		
		menuAdapter = new MainMenu(this, thumbPathList, comicNameList, comicEntryList);
		gv.setAdapter(menuAdapter);
		gv.setOnItemClickListener(this);

	}
	
	private String getComicPosterPathByEntry(String entry) {
		return FileManager.MCK_VIEW_CACHE_DIR+"/"+entry+"/poster.jpg";
	}
	
	private String getComicNameByEntry(String entry)
	{
		char[] buf = new char[10240];
		FileReader r;
		
		try {
			r = new FileReader(FileManager.MCK_VIEW_CACHE_DIR+"/"+entry+"/info.txt");
			r.read(buf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String content = new String(buf);
		int tokenStartIndexNameShow = content.indexOf("name_show:");
		int tokenEndIndexNameShow = content.indexOf("\n", tokenStartIndexNameShow);
		return content.substring(tokenStartIndexNameShow+10,
				tokenEndIndexNameShow > 0? tokenEndIndexNameShow-1 : buf.length-1)
				.trim();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		String itemEntry = menuAdapter.getItemEntry(arg2);

		Intent comicView = new Intent(this, ComicView.class);
		comicView.putExtra("COMIC_ENTRY", itemEntry);
		this.startActivity(comicView);
	}

}